﻿@model SAKS.ViewModels.ProjectDetailsModel

@{
    ViewBag.Title = "项目 - "+ Model.Name;
    Layout = "_ProjectLayout.cshtml";
}
<input type="hidden" id="hidden-prpjectid" value="@Model.Id" />
<div class="display-box">
    <div class="display-label">项目序号</div>
    <div class="display-field">@Model.SerialNumber</div>

    <div class="display-label">项目名称</div>
    <div class="display-field">@Model.Name</div>

    <div class="display-label">项目级别</div>
    <div class="display-field">@Model.ProjectLevel</div>

    <div class="display-label">领域</div>
    <div class="display-field">@Model.SpecializedField</div>

    <div class="display-label">起始时间</div>
    <div class="display-field">@Model.StartDate</div>

    <div class="display-label">结束时间</div>
    <div class="display-field">@Model.EndDate</div>

    <div class="display-label">负责人</div>
    <div class="display-field"><a class="realname" href="#">@Model.ResponsibleOfficer</a></div>

    <div class="display-label">总设计师</div>
    <div class="display-field"><a class="realname" href="#">@Model.PrimaryDesigner</a></div>

    <div class="display-label">状态</div>
    <div class="display-field">@Model.State</div>

    <div class="display-label">承担单位</div>
    <div class="display-field">
        @foreach (var item in Model.Organizations)
        {
            <span><a class="organization" href="#">@item</a> </span>
        }
    </div>

    <div class="display-label">项目人员</div>
    <div class="display-field">
        @foreach (var item in Model.Members)
        {
            <span><a class="realname" href="#">@item </a></span> 
        }
    </div>

    <div class="display-label">摘要</div>
    <div class="display-field">@Model.Summary</div>
</div>

@if (ViewBag.HasJoined == false)
{ 
    <p><a href="#" id="join" onclick="openjoinDialog()">申请加入</a></p>
         @Html.ActionLink("返回", "Index")
}
else
{
    <p><a href="#" id="quit" onclick="openQuitDialog()">退出项目</a></p>
    <p id="edit">
         @Html.ActionLink("编辑", "Edit", new { id = Model.Id }) 
         @Html.Raw("|")
         @Html.ActionLink("返回", "Index")
    </p>
}

<div id="joinDialog">
    <label>
        <span>备注</span>
        <textarea cols="8" rows="4" id="joinMessage"></textarea>
    </label>
    <div id="responseMessage"></div>
</div>

<div id="quitDialog">你确定要退出吗？</div>

<div id="hasPostRequest">请不要重复提交请求。</div>


<script type="text/javascript">
    var hasPostRequest = false;
    var btnJoinClicked = false;
    var btnQuitClicked = false;
    function openjoinDialog() {
        if (hasPostRequest == false) {
            $("#joinDialog").dialog("open");
            
        } else
            $("#hasPostRequest").dialog("open");
    }


    function openQuitDialog() {
        $("#quitDialog").dialog("open");
    }

    function sendJoinRequest() {
        if (btnJoinClicked == false) {
            var label = $("#joinDialog label").hide();
            var responseMsg = $("#responseMessage").text("请求发送中...");
            var message = $("#joinMessage").get(0).value;
            var id = $("#hidden-prpjectid").get(0).value;
            $.post(
                "/Project/Join",
                { "Id": id, "Message": message },
                function (data) {
                    if (data.Result == 'Ok') {
                        var responseMsg = $("#responseMessage").text("请求发送成功，请等待管理员批准。");
                        hasPostRequest = true;
                    }
                    else
                        $("#responseMessage").text(data.result);
                }
            );
            btnJoinClicked = true;
        } else {
            $("#joinDialog").dialog("close");
            btnJoinClicked = false;
        }

    }
    $(function () {
        $("#hasPostRequest").dialog({
            title: "请求已经发出",
            autoOpen: false,
            modal: true,
            buttons: [
                { text: '关闭', click: function () { $(this).dialog("close") } }
            ]
        });
        $("#quitDialog").dialog({
            autoOpen: false,
            modal: true,
            position: 'center',
            title: '退出项目',
            buttons: [
                {
                    text: "确认",
                    click: function(){
                        if(btnQuitClicked == false){
                            $("#quitDialog").text("请求发送中...");
                            $.get(
                                "/Project/Quit/"+$("#hidden-prpjectid").get(0).value,
                                {},
                                function(data){
                                    $("#quitDialog").text(data);
                                    if(data=="退出成功"){
                                        $("#quit").removeAttr("onclick");
                                        $("#quit").text("申请加入").click(openjoinDialog);
                                        $("#edit").hide();
                                        $("#quitDialog").dialog("close");
                                    }                             
                                }
                            );
                            btnQuitClicked = true;
                        }else{
                            $("#quitDialog").dialog("close");
                            btnQuitClicked = false;
                            $("#quitDialog").text("你确定要退出吗？");
                        }
                    }
                },
                {
                    text: "关闭",
                    click: function () { $(this).dialog("close"); }
                }
            ]
        });
        $("#joinDialog").dialog({
            autoOpen: false,
            modal: true,
            position: 'center',
            title: '项目加入申请',
            buttons: [
                {
                    text: "确认",
                    click: sendJoinRequest
                },
                {
                    text: "关闭",
                    click: function () { $(this).dialog("close"); }
                }
            ],
            close: function (event, ui) {
                $("#joinDialog label").show();
                $("#joinDialog textarea").text("");
                $("#responseMessage").text("");
            }

        });
    });
</script>